﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.SellOrder;
using System.Collections;
using UI.Lib.Factory;
using UI.Lib;

namespace UI.StorageManagement
{
    public partial class FrmOrdBack : UI.ParentForm.BaseMain
    {
        FrmOrdSendMedit edit = null;
        CheckBox chkBackDoc = null; //仅未回单输入

        public FrmOrdBack(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();
        }

        protected override void AddToolButton()
        {
            int firstBtnIndex = this.tspTop.Items.IndexOf(tspAdd);

            this.tspTop.Items.Add(base.CreateToolBtn("B", "回单输入", new EventHandler(OnBackBtnClick)));

            //仅未回单输入
            this.tspTop.Items.Add(base.CreateToolCheckBox("CB", "仅未回单输入", false, "",
                  new EventHandler(DoBinder), out chkBackDoc));
        }

        void DoBinder(object sender, EventArgs e)
        {
            BindGrid();
        }

        //点击回单输入后执行
        void OnBackBtnClick(object sender, EventArgs e)
        {
            DataGridViewRow row = this.GetSelectedRow();
            if (null == row) return;

            edit = new FrmOrdSendMedit(this.ModuleCode, "B", GetTitleStyle(this.Text, "回单录入"),
                Components.Validation.ToString(row.Cells["listid"].Value));

            edit.ShowDialog();
            if (edit.IsReload) base.BindGrid();
        }

        protected override void OnViewClick(ToolStripButton btn, DataGridViewRow row)
        {
            if (null == row) return;

            edit = new FrmOrdSendMedit(this.ModuleCode, "V", GetTitleStyle(this.Text, btn.Text),
                Components.Validation.ToString(row.Cells["listid"].Value));

            edit.ShowDialog();
        }

        protected override void UpdatePager(UI.Common.Pager pager)
        {
            pager.FilterAppName = "a.muser";

            //未删除且审核过的订单才能进行发货处理
            pager.StrWhere = " a.status IN ('2','3','4','5') AND a.audstatus = '2' ";

            if (null != chkBackDoc && chkBackDoc.Checked == true)
            {
                pager.StrWhere += " AND a.status IN ('2','3','4') ";
            }
        }

        /// <summary>
        /// 行改变时触发
        /// </summary>
        /// <param name="rowIndex"></param>
        protected override void AfterRowChanged(int rowIndex)
        {
            if (rowIndex == -1) return;

            string status = Components.Validation.ToString(this.dgvMain["audstatus", rowIndex].Value);

            ToolStripItem[] items = null;

            items = this.tspTop.Items.Find("btn_B", false);
            ToolStripItem tspBack = (0 == items.Length) ? null : items[0];

            tspAdd.Enabled = false;
            if (tspBack != null) tspBack.Enabled = false;

            if ("3" == status || "2" == status)
            {
                if (tspBack != null) tspBack.Enabled = true;
            }
        }
    }
}
